package com.blue.waffle.iwc.screens;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.sql.SQLException;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.blue.waffle.iwc.domains.Difficulty;
import com.blue.waffle.iwc.domains.Word;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.ui.ButtonActorListener;
import com.blue.waffle.iwc.ui.ParallaxBackground;
import com.blue.waffle.iwc.ui.ParallaxLayer;

public class DictionaryScreen extends AbstractScreen {

	private static final float WINDOW_PADDINGS = 60;
	private static final float CELL_PADDINGS = 5;
	private static final float ROWS = 1;
	private static final int COLS = 2;
	private Window window;
	private String fileDirectoryPath;
	private File fileDir;
	private Label pathLabel;
	private File[] currentDirFileList;
	
	private static final FilenameFilter FILENAME_FILTER=new FilenameFilter() {
		@Override
		public boolean accept(File dir,String name) {
			if (dir.canRead()) {
				if (dir.isDirectory()) {
					return !name.startsWith(".");
				}else {
					return !name.startsWith(".");
				}
			}
			return false;
		}
	};
	
	
	public DictionaryScreen(IWCGame game,String filePath) {
		super(game);
		this.fileDirectoryPath=filePath.substring(0, filePath.length()-1);
		fileDir=new File(filePath);
		if (!fileDir.exists()) {
			fileDir.mkdir();
		}
	}
	
	@Override
	public void show() {
		super.show();
		Skin skin = getUiSkin();
		
		window = new Window("Add Custom Dictionary", skin);
		gameStage.addActor(window);
		window.setMovable(false);
		window.setBounds(WINDOW_PADDINGS, WINDOW_PADDINGS,
				IWCGame.VIRTUAL_WIDTH - WINDOW_PADDINGS * 2,
				IWCGame.VIRTUAL_HEIGHT - WINDOW_PADDINGS * 2);
		window.defaults().fill().padBottom(CELL_PADDINGS);
		
		float rowHeight = (window.getHeight()-40) / ROWS - CELL_PADDINGS * 2;
		
		pathLabel=new Label(fileDir.getName(), skin);
		window.row();
		window.add(pathLabel).colspan(COLS);
		// new row
		window.row().height(rowHeight).fillX();
		
		currentDirFileList=fileDir.listFiles(FILENAME_FILTER);
		final List list=new List(currentDirFileList==null?new String[]{}:fileDir.list(FILENAME_FILTER), skin);
		final ScrollPane scrollPane=new ScrollPane(list, skin);
		
		window.add(scrollPane).size(400,rowHeight-50);
	
		final TextButton upButton=new TextButton("Up", skin);
		
		window.add(upButton).top().right().height(70);
		
		setupFileListListener(list, upButton);
		
		gameStage.addActor(getButtonTable(skin, new ButtonActorListener(this){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				screen.game.setScreen(new OptionsScreen(screen.game));
			}
		}).right().bottom());
	}
	
	private void setupFileListListener(final List list, final TextButton upButton){
		upButton.setDisabled(true);
		upButton.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (!upButton.isDisabled()) {
					fileDir=fileDir.getParentFile();
					if (fileDir.getAbsolutePath().equalsIgnoreCase(fileDirectoryPath)) {
						upButton.setDisabled(true);
					}
					String[] fileNames=fileDir.list(FILENAME_FILTER);
					if (fileNames==null) {
						fileNames=new String[1];
						fileNames[0]="";
					}
					currentDirFileList=fileDir.listFiles(FILENAME_FILTER);
					list.setItems(fileNames);
				}
				
			}
		});
		
		list.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				
				int selectedIndex=list.getSelectedIndex();
				if (selectedIndex>=0) {
					File selectedDirFile = currentDirFileList[selectedIndex];
					if (selectedDirFile.isDirectory()) {
						fileDir=selectedDirFile;
						currentDirFileList=selectedDirFile.listFiles(FILENAME_FILTER);
						String[] fileNames=fileDir.list(FILENAME_FILTER);
						if (fileNames==null) {
							fileNames=new String[1];
							fileNames[0]="";
						}
						list.setItems(fileNames);
						upButton.setDisabled(false);
					}else {
						System.out.println(selectedDirFile.getName());
						try {
							game.getDatabaseHelper().getWordHelper().insertWordsFromCSV(new FileReader(selectedDirFile));
							java.util.List<Word> words=game.getDatabaseHelper().getWordHelper().selectAll();
							java.util.List<Difficulty> difficulties=game.getDatabaseHelper().getDifficultyHelper().selectAll();
							
							game.getDatabaseHelper().getLevelHelper().insertLevels(words, difficulties);
							
						} catch (IOException e) {
							e.printStackTrace();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
				
			}
		});
	}
	
	

}
